package com.company.service.impl;

import com.company.dao.PermissionDao;
import com.company.dao.RoleDao;
import com.company.dao.UserDao;
import com.company.pojo.Permission;
import com.company.pojo.Role;
import com.company.pojo.User;
import com.company.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/**
 * @author fu xiao liu
 * @date 2021-08-16 21:08
 */
@DubboService(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    /**
     * 根据用户名查询数据库获取用户信息和关联的角色信息，同时需要查询角色关联的权限信息
     * @param username
     * @return
     */
    @Override
    public User findByUsername(String username) {
        // 查询用户基本信息，不包含用户的角色
        User user = userDao.findByUsername(username);
        if(user == null){
            return null;
        }
        Integer userId = user.getId();
        // 根据用户ID查询对应的角色
        Set<Role> roles = roleDao.findByUserId(userId);
        for (Role role : roles) {
            Integer roleId = role.getId();
            // 根据角色ID查询关联的权限
            Set<Permission> permissions = permissionDao.findByRoleId(roleId);
            // 让角色关联权限
            role.setPermissions(permissions);
        }
        // 让用户关联角色
        user.setRoles(roles);
        return user;
    }
}